package com.example.actflow;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.viewpager2.widget.ViewPager2;

import com.example.actflow.fragment.AppListFragment;
import com.example.actflow.fragment.FlowConfigFragment;
import com.example.actflow.model.AppInfo;
import com.example.actflow.repository.ContextData;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

public class MainActivity extends AppCompatActivity {


    private ViewPager2 viewPager;
    private TabLayout tabLayout;

    private AppListFragment appListFragment;
    private FlowConfigFragment flowConfigFragment;

    private ContextData contextData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });


        contextData = ContextData.getInstance(this);

        viewPager = findViewById(R.id.view_pager);
        tabLayout = findViewById(R.id.tab_layout);

        appListFragment = new AppListFragment();
        flowConfigFragment = new FlowConfigFragment();


        appListFragment.setOnAppListChangedListener(appInfo -> {
            if (appInfo != null) {
                viewPager.setCurrentItem(1, true);
                contextData.loadPackage(appInfo);
                flowConfigFragment.updateView();
            }
            flowConfigFragment.updateView();
        });

        viewPager.setAdapter(new androidx.viewpager2.adapter.FragmentStateAdapter(this) {
            @NonNull
            @Override
            public androidx.fragment.app.Fragment createFragment(int position) {
                return (position == 0) ? appListFragment : flowConfigFragment;
            }

            @Override
            public int getItemCount() {
                return 2;
            }
        });

        new TabLayoutMediator(tabLayout, viewPager,
                (tab, position) -> {
                    tab.setText(position == 0 ? "应用列表" : "流程配置");
                }).attach();
    }

    public void launchApp(String packageName) {
        PackageManager packageManager = getPackageManager();
        Intent intent = packageManager.getLaunchIntentForPackage(packageName);

        if (intent != null) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        } else {
            Toast.makeText(this, "Application not found or cannot be launched.", Toast.LENGTH_SHORT).show();
        }
    }

}
